package com.xu.crawler.processor;

import com.xu.crawler.dao.HistoryRecordDao;
import com.xu.crawler.dao.HkStockDao;
import com.xu.crawler.dao.StockHolderDao;
import com.xu.crawler.dao.entity.HistoryRecordDO;
import com.xu.crawler.dao.entity.NewsDO;
import com.xu.crawler.dao.entity.StockHolderDO;
import com.xu.crawler.enums.URLMapperEnum;
import com.xu.crawler.pipeline.MongoPipeline;
import com.xu.crawler.util.SpringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.assertj.core.util.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.enums.SourceEnum;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.utils.UrlUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Locale;

/**
 * 股票持仓列表
 * <p>
 * Created by clear on 2018/7/11.
 */
@Service
public class StockHolderPageProcessor implements PageProcessor {

    private Logger log = LoggerFactory.getLogger(StockHolderPageProcessor.class);
    private Site site = Site.me()
            .setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36")
            .setDomain(URLMapperEnum.TONGHUASHUN.toString())
            .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
            .addHeader("Accept-Encoding", "gzip, deflate")
            .addHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
            .addHeader("Cache-Control", "no-cache")
            .addHeader("Cookie", "cmsad_169_0=0; Hm_lvt_f79b64788a4e377c608617fba4c736e2=1534648297,1534648412; Hm_lvt_22a3c65fd214b0d5fd3a923be29458c7=1534648297,1534648412; Hm_lvt_78c58f01938e4d85eaf619eae71b4ed1=1534648161,1534652870; cmsad_170_0=0; cmsad_171_0=0; cmsad_172_0=0; spversion=20130314; historystock=HK0002%7C*%7CHK0001%7C*%7CHK2330%7C*%7C000001; log=; user=MDp4dWd1YW5nd3UyNDo6Tm9uZTo1MDA6NDI3NzczNDExOjcsMTExMTExMTExMTEsNDA7NDQsMTEsNDA7NiwxLDQwOzUsMSw0MDsxLDEsNDA7MiwxLDQwOzMsMSw0MDs1LDEsNDA7OCwwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMSw0MDoyNzo6OjQxNzc3MzQxMToxNTM0NzI2NzIxOjo6MTUwNzU1Nzk2MDo2MDQ4MDA6MDoxYTAxY2IwODkwNmZjYTA4YzVlNjI1ZDliYzFmZGM5Zjk6ZGVmYXVsdF8yOjA%3D; userid=417773411; u_name=xuguangwu24; escapename=xuguangwu24; ticket=fbbabca01545aaa6e532536f47d8cd88; Hm_lpvt_22a3c65fd214b0d5fd3a923be29458c7=1534726809; Hm_lpvt_f79b64788a4e377c608617fba4c736e2=1534726809; Hm_lpvt_78c58f01938e4d85eaf619eae71b4ed1=1534726809; v=AvWVD3akws7sMiZy9VKzCXifAnqscqmTcyaN2HcasWy7ThrsP8K5VAN2nawE")
            .addHeader("Host", "stockpage.10jqka.com.cn")
            .addHeader("Pragma", "no-cache")
            .addHeader("Proxy-Connection", "keep-alive")
            .addHeader("Upgrade-Insecure-Requests", "1")
            .setRetryTimes(3).setSleepTime(1000);

    @Resource
    private StockHolderDao stockHolderDao;

    @Override
    public void process(Page page) {
        try {
            Html html = page.getHtml();
            String stockCode = html.xpath("//h1[@class='m_logo fl']/a/text()").get();
            String time = html.xpath("//div[@stat='holder_main']/div[@class='bd pt5']/p[1]/allText()").get();
            List<String> holders = html.xpath("//div[@stat='holder_main']/div[@class='bd pt5']/table[1]/tbody//td/allText()").all();
            List<String> holderNames = html.xpath("//div[@stat='holder_main']/div[@class='bd pt5']/table[1]/tbody//th/allText()").all();
            if (stockHolderDao == null) {
                stockHolderDao = SpringUtils.getBean(StockHolderDao.class);
            }
            List<StockHolderDO> stockHolderDOList = Lists.newArrayList();
            StockHolderDO stockHolderDO;
            for (int i = 0; i < holderNames.size(); i++) {
                stockHolderDO = new StockHolderDO();
                stockHolderDO.setHolderName(holderNames.get(i));
                stockHolderDO.setTargetHolder(holders.get(i * 5 + 0));
                stockHolderDO.setHoldNum(holders.get(i * 5 + 1));
                stockHolderDO.setHoldPercent(holders.get(i * 5 + 2));
                stockHolderDO.setHoldState(holders.get(i * 5 + 3));
                stockHolderDO.setStockType(holders.get(i * 5 + 4));
                stockHolderDO.setHoldTime(time);
                stockHolderDO.setStockCode(stockCode.trim());
                stockHolderDOList.add(stockHolderDO);
            }
            stockHolderDao.batchSave(stockHolderDOList);
        } catch (Throwable e) {
            log.error("process fail {}", e);
        }
    }

    @Override
    public Site getSite() {
        return site;
    }


}
